package com.itheima.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.pojo.Category;

import com.itheima.service.CategoryService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Slf4j
@RequestMapping("/category")
@RestController
public class CategoryConterllor {
    @Autowired
    private CategoryService categoryService;

    /*
     * 分页查询*/
    @GetMapping("/page")
    public R<Page> selectPage(int page, int pageSize) {
        Page<Category> categoryPage = new Page<>(page, pageSize);
        categoryService.page(categoryPage);
        return R.success(categoryPage);
    }

    /*
     *新增分类 */
    @PostMapping
    public R<String> save(@RequestBody Category category) {
        log.info("新增菜品{}", category.toString());
        categoryService.save(category);
        return R.success("新增菜品成功");
    }

    /*
     * 根据id删除分类*/
    @DeleteMapping
    public R<String> delete(Long id) {

        //categoryService.removeById(id);
        categoryService.remove(id);

        return R.success("分类信息删除成功");
    }

    /*
     * 修改分类*/
    @PutMapping
    public R<String> update(@RequestBody Category category) {
        categoryService.updateById(category);
        return R.success("修改成功");

    }
    /*
     * 查询菜品分类列表数据*/
    @GetMapping("/list")
    public R<List<Category>> getlist(Category category){

        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(category.getType()!=null,Category::getType,category.getType());
        wrapper.orderByDesc(Category::getSort).orderByDesc(Category::getUpdateTime);
        List<Category> list = categoryService.list(wrapper);
        return R.success(list);
    }
}
